/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ad.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ad.dao.AdAgentDao;
import com.jeesite.modules.ad.dao.AdDao;
import com.jeesite.modules.ad.dao.AdGuestDao;
import com.jeesite.modules.ad.dao.AdVertiserDao;
import com.jeesite.modules.ad.entity.Ad;
import com.jeesite.modules.ad.entity.AdAgent;
import com.jeesite.modules.ad.entity.AdGuest;
import com.jeesite.modules.ad.entity.AdVertiser;
import com.jeesite.modules.ad.service.AdAgentService;
import com.jeesite.modules.ad.service.AdVertiserService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;

/**
 * 广告系统桌面控制层
 * 
 * @author 蒋佳俊
 * @version 2019/06/13
 *
 */
@Controller
@RequestMapping(value = "${adminPath}/ad/adSysDesktop")
public class AdSysDesktopController extends BaseController {

	@Autowired
	private AdAgentService adAgentService;
	@Autowired
	private AdAgentDao adAgentDao;
	@Autowired
	private AdDao adDao;
	@Autowired
	private AdGuestDao adGuestDao;

	@Autowired
	private AdVertiserService adVertiserService;
	@Autowired
	private AdVertiserDao adVertiserDao;

	static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 广告系统桌面
	 * 
	 * @return
	 */
	@RequestMapping(value = "adSysDesktop")
	public String adSysDesktop(Model model) {
		// 直属代理商
		long findCount = 0;
		// 所有代理商
		long allFindCount = 0;
		// 直属广告主
		long adVertiserFindCount = 0;
		// 所有广告主
		long adVertiserFindCountAll = 0;
		// 广告计划
		long adAllCount = 0;
		// 获客量
		long adGuestFindCount = 0;
		// 展示图
		List<Map<String, Object>> resultMap = null;
		// 广告曝光量
		List<Double> adExposureNum = new ArrayList<Double>(16);
		// 广告点击量
		List<Double> adClickNum = new ArrayList<Double>(16);
		// 落地页展示量
		List<Double> ldShowNum = new ArrayList<Double>(16);
		// 获客数量
		List<Double> count = new ArrayList<Double>(16);
		// 时间
		List<String> censusDate = new ArrayList<String>(16);

		User user = UserUtils.getUser();
		String userCode = user.getUserCode();
		// 代理商
		AdAgent adAgentWhere = new AdAgent();
		adAgentWhere.setUserCode(userCode);
		AdAgent adAgentResult = adAgentDao.getByEntity(adAgentWhere);
		// 广告主信息
		AdVertiser adVertiserWhere1 = new AdVertiser();
		adVertiserWhere1.setUserCode(userCode);
		AdVertiser adVertiserResult = adVertiserDao.getByEntity(adVertiserWhere1);
		if (adAgentResult != null) {
			// 获取当前代理商id
			String id = adAgentResult.getId();
			AdAgent where1 = new AdAgent();
			where1.setUpperAgent(id);
			// 广告主
			AdVertiser adVertiserWhere = new AdVertiser();
			adVertiserWhere.setAgentId(id);
			// 直属代理商
			findCount = adAgentDao.findCount(where1);
			// 所有代理商
			allFindCount = adAgentDao.findAgentCountByUpperAgents(id);
			// 直属广告主
			adVertiserFindCount = adVertiserDao.findCount(adVertiserWhere);
			// 所有广告主
			adVertiserFindCountAll = adVertiserDao.findAllAdVertiserByAgent(id);
			// 广告计划
			adAllCount = adDao.findCountByAgent(id);
			// 获客量
			adGuestFindCount = adGuestDao.findCountAll(id);
			resultMap = adAgentDao.find(id);
			for (Map<String, Object> string : resultMap) {
				adExposureNum.add(Double.valueOf(string.get("adExposureNum").toString()));
				adClickNum.add(Double.valueOf(string.get("adClickNum").toString()));
				ldShowNum.add(Double.valueOf(string.get("ldShowNum").toString()));
				count.add(Double.valueOf(string.get("count").toString()));
				censusDate.add("'" + string.get("date") + "'");
			}
			// 广告主
		} else if (adVertiserResult != null) {
			Ad where = new Ad();
			where.setVertiserId(adVertiserResult.getId());
			// 广告计划
			adAllCount = adDao.findCount(where);
			// 获客量
			AdGuest adGuestWhere = new AdGuest();
			adGuestWhere.setAdVertiserId(adVertiserResult.getId());
			adGuestFindCount = adGuestDao.findCount(adGuestWhere);
			resultMap = adAgentDao.findCountByVertiser(adVertiserResult.getId());
			for (Map<String, Object> string : resultMap) {
				adExposureNum.add(Double.valueOf(string.get("adExposureNum").toString()));
				adClickNum.add(Double.valueOf(string.get("adClickNum").toString()));
				ldShowNum.add(Double.valueOf(string.get("ldShowNum").toString()));
				count.add(Double.valueOf(string.get("count").toString()));
				censusDate.add("'" + string.get("date") + "'");
			}
		} else {
			AdAgent where1 = new AdAgent();
			where1.setCreateBy(userCode);
			// 广告主
			AdVertiser adVertiserWhere = new AdVertiser();
			adVertiserWhere.setCreateBy(userCode);
			// 直属代理商
			findCount = adAgentDao.findCount(where1);
			// 所有代理商
			allFindCount = adAgentDao.findCount(new AdAgent());
			// 直属广告主
			adVertiserFindCount = adVertiserDao.findCount(adVertiserWhere);
			// 所有广告主
			adVertiserFindCountAll = adVertiserDao.findCount(new AdVertiser());
			// 广告计划
			adAllCount = adDao.findCount(new Ad());
			// 获客量
			adGuestFindCount = adGuestDao.findCount(new AdGuest());
			resultMap = adAgentDao.findCountByGuanLiYuan();
			for (Map<String, Object> string : resultMap) {
				adExposureNum.add(Double.valueOf(string.get("adExposureNum").toString()));
				adClickNum.add(Double.valueOf(string.get("adClickNum").toString()));
				ldShowNum.add(Double.valueOf(string.get("ldShowNum").toString()));
				count.add(Double.valueOf(string.get("count").toString()));
				censusDate.add("'" + string.get("date") + "'");
			}
		}
		model.addAttribute("findCount", findCount);
		model.addAttribute("allFindCount", allFindCount);
		model.addAttribute("adVertiserFindCount", adVertiserFindCount);
		model.addAttribute("adVertiserFindCountAll", adVertiserFindCountAll);
		model.addAttribute("adAllCount", adAllCount);
		model.addAttribute("adGuestFindCount", adGuestFindCount);
		Date date = new Date();
		String startDate = getMinMonthDate(date);
		String endDate = getMaxMonthDate(date);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);

		// 仪表盘
		model.addAttribute("adExposureNum", adExposureNum);
		model.addAttribute("adClickNum", adClickNum);
		model.addAttribute("ldShowNum", ldShowNum);
		model.addAttribute("count", count);
		model.addAttribute("censusDate", censusDate);
		return "modules/ad/adSysDesktop";
	}

	/**
	 * 获取月份起始日期
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMinMonthDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 获取月份最后日期
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMaxMonthDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}

}